package com.zhuzi.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @author study
 * @version 1.0
 * @date 2020/8/8 22:07
 */
@Configuration
public class ThreadPoolConfig {

    // 线程池设置 https://cdn.jsdelivr.net/gh/qianlicao51/files@main/img/2022-06/20220628221216.png
    @Bean(value = "threadPool")
    public ExecutorService threadPool() {
        //是处理器的核的数目
        int processors = Runtime.getRuntime().availableProcessors();
        ThreadFactory nameThreadFactory = new ThreadFactoryBuilder().setNameFormat("sys-queue-thread-%d").build();
        ExecutorService pool = new ThreadPoolExecutor(processors / 2, processors * 3, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(50), nameThreadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
        return pool;
    }
}
